import express from 'express'
import swagger from 'express-swagger-generator'
import InitRouterController from './controller/index'
import bodyParser from 'body-parser'
console.log(bodyParser)
const serverConfig = require('./server.config.json')

class App {
  public app: express.Application
  constructor() {
    this.app = express()
    this.app.get('/', (req, res) => {
      res.send({message: 'hello express'})
    })
    this.init()
  }

  private init() {
    this.app.use(bodyParser.json())
    this.app.use(
      bodyParser.urlencoded({
        extended: false,
      })
    )
    this.swaggerConfig()
    this.app.use(InitRouterController)
  }

  private swaggerConfig() {
    const expressSwagger = swagger(this.app)
    //#region swagger基础配置
    let options = {
      swaggerDefinition: {
        // info: {
        //   description: 'This is a sample server',
        //   title: 'Swagger',
        //   version: '1.0.0',
        // },
        // host: 'localhost:9000',
        basePath: '/',
        produces: ['application/json', 'application/xml'],
        schemes: ['http'],
        securityDefinitions: {
          JWT: {
            type: 'apiKey',
            in: 'header',
            name: 'Authorization',
            description: '',
          },
        },
      },
      route: {
        url: '/swagger/index.html',
        docs: '/swagger.json', //swagger文件 api
      },
      basedir: __dirname, //app absolute path
      files: ['./controller/*.ts', 'swagger.desc.ts'], //Path to the API handle folder
    }
    //#endregion

    expressSwagger(options)
    this.app.use('/api-docs.json', function (req, res) {
      res.json(require('./path/to/swaggerize/docs.json'))
    })
    this.app.use('/api-docs', swagger())
    console.log('>暂未编写swagger注释<')
    console.log('\x1B[36m')
    console.log(
      `> swagger--> http://localhost:${serverConfig.WEB_API.PORT}/swagger/index.html`
    )
    console.log()
  }
}

export default new App().app
